package com.example.rabbit.forth;

import com.example.rabbit.util.RabbitMqUtils;
import com.example.rabbit.util.SleepUtil;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @Author ifredomvip@gmail.com
 * @Date 2022/8/8 9:29
 * @Version 1.0.0
 * @Description
 **/
public class PublicConsumer1 {
    public static void main(String[] args) {
        try {
            Channel channel = RabbitMqUtils.getChannel();
            String queueName = "publicConfirm";

            DeliverCallback deliverCallback = (consumeTag, deliver) -> {

                System.out.println("工作线程等待。。。");

                SleepUtil.sleep(30);
                System.out.println("收到消息" + consumeTag + "=====" + new String(deliver.getBody(), StandardCharsets.UTF_8));
                channel.basicAck(deliver.getEnvelope().getDeliveryTag(), false);
            };

            CancelCallback cancelCallback = consumeTag -> {
                System.out.println("取消应答" + consumeTag);
            };

            channel.basicConsume(queueName, false, deliverCallback, cancelCallback);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
